package top.lauwen.handler;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnStart = null;
    private Button btnStop = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = (Button) findViewById(R.id.btnStart);
        btnStart.setOnClickListener(new btnStratListener());
        btnStop = (Button) findViewById(R.id.btnStop);
        btnStop.setOnClickListener(new btnStopListener());
    }
    Handler handler = new Handler();
    class btnStratListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            handler.post(updateThread);
        }
    }
    class  btnStopListener implements View.OnClickListener{
        @Override
        public void onClick(View v){
            handler.removeCallbacks(updateThread);
        }
    }
    Runnable updateThread = new Runnable() {
        @Override
        public void run() {
            System.out.println("updateThread");
            Log.v("TAG","---->UPDATETHREAD");
            handler.postDelayed(updateThread,2000);
        }
    };
}
